package top.ranzhang.service.impl;

import com.baomidou.mybatisplus.extension.toolkit.Db;
import org.springframework.beans.BeanUtils;
import org.springframework.util.DigestUtils;
import top.ranzhang.bean.dto.UserLoginDTO;
import top.ranzhang.bean.entity.Child;
import top.ranzhang.bean.entity.User;
import top.ranzhang.bean.vo.UserLoginVO;
import top.ranzhang.bean.vo.UserToChildVO;
import top.ranzhang.exception.BaseException;
import top.ranzhang.mapper.UserMapper;
import top.ranzhang.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ran
 * @since 2025-04-14
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public UserLoginVO login(UserLoginDTO userLoginDTO) {
        //因为是微信小程序所有就不做密码加密和拦截器
        User one = lambdaQuery().eq(User::getUsername, userLoginDTO.getUsername()).one();
        if(one == null){
            throw new BaseException("账号名不存在");
        }
        if(!one.getPassword().equals(userLoginDTO.getPassword())){
            throw new BaseException("密码错误");
        }
        UserLoginVO userLoginVO = new UserLoginVO();
        BeanUtils.copyProperties(one,userLoginVO);
        return userLoginVO;
    }

    @Override
    public UserToChildVO getByUserId(Integer id) {
        User one = Db.lambdaQuery(User.class).eq(User::getId, id).one();
        List<Child> list = Db.lambdaQuery(Child.class).eq(Child::getUserId, id).list();
        UserToChildVO userToChildVO = new UserToChildVO();
        BeanUtils.copyProperties(one,userToChildVO);
        userToChildVO.setChildList(list);
        return userToChildVO;
    }
}
